<template>
  <div class="app-container">
	
    <el-row :gutter="10" class="mb8" style="height: 30px;">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['material:record:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['material:record:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['material:record:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['material:record:export']"
        >导出</el-button>
      </el-col>

	 <el-col :span="1.5">
		 <div style="margin-left: 505px;">
		  <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
		    <el-form-item label="产品编号" prop="productNumber">
		      <el-input
		        v-model="queryParams.productNumber"
		        placeholder="请输入产品编号"
		        clearable
		        @keyup.enter.native="handleQuery"
		      />
		    </el-form-item>
		  
		    <el-form-item>
		      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
		      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
		    </el-form-item>
		  </el-form>
		  </div>
	</el-col>  

    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange" border >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="产品编号" align="center" prop="productNumber" width="180"/>
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="产品型号" align="center" prop="productModel" />
      <el-table-column label="产品类型" align="center" prop="productType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_product_category" :value="scope.row.productType"/>
        </template>
      </el-table-column>
      <el-table-column label="产品分类" align="center" prop="productCategory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_product_classification" :value="scope.row.productCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="单位" align="center" prop="unit">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_unit" :value="scope.row.unit"/>
        </template>
      </el-table-column>
      <el-table-column label="规格" align="center" prop="specification" />
      <el-table-column label="材料" align="center" prop="material" />
      <el-table-column label="颜色" align="center" prop="color" />
      <el-table-column label="图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
	  <el-table-column label="提交人" align="center" prop="sysUser.userName"/>
      <el-table-column label="提交时间" align="center" prop="submissionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submissionTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['material:record:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['material:record:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料档案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
		  
		<el-row style="margin-bottom: 20px;">
		  	<el-col>
				<el-form-item label="产品编号" label-width="50" style="margin: 0px;"/>
				<el-input  placeholder="自动生成无需填写" disabled style="width: 75%;"/>
		  	</el-col>
		</el-row>
		  
		<el-row style="margin-bottom: 20px;">
		  	<el-col>
				<el-form-item label="产品名称" prop="productName" label-width="50" style="margin: 0px;"/>
				<el-input v-model="form.productName" placeholder="请输入产品名称" style="width: 75%;"/>
		  	</el-col>
		</el-row>
		
		<el-row style="margin-bottom: 20px;">
		  	<el-col>
				<el-form-item label="产品型号" prop="productModel" label-width="50" style="margin: 0px;"/>
				<el-input v-model="form.productModel" placeholder="请输入产品型号" style="width: 75%;"/>
		  	</el-col>
		</el-row>
        
		<el-row style="margin-bottom: 20px;">
		  	<el-col>
			<el-form-item label="产品类型" prop="productType" label-width="50" style="margin: 0px;"/>
			  <el-select v-model="form.productType" placeholder="请选择产品类型" style="width: 75%;">
				<el-option
				  v-for="dict in dict.type.sys_product_category"
				  :key="dict.value"
				  :label="dict.label"
				  :value="parseInt(dict.value)"
				></el-option>
			  </el-select>
			</el-col>
		</el-row>
		
		<el-row style="margin-bottom: 20px;">
			<el-col>
			<el-form-item label="产品分类" prop="productCategory" label-width="50" style="margin: 0px;"/>
			  <el-select v-model="form.productCategory" placeholder="请选择产品分类" style="width: 75%;">
				<el-option
				  v-for="dict in dict.type.sys_product_classification"
				  :key="dict.value"
				  :label="dict.label"
				  :value="parseInt(dict.value)"
				></el-option>
			  </el-select>
			</el-col>
		</el-row>
		
		<el-row style="margin-bottom: 20px;">
			<el-col>
				<el-form-item label="产品单位" prop="unit" label-width="50" style="margin: 0px;"/>
				  <el-select v-model="form.unit" placeholder="请选择单位" style="width: 75%;">
					<el-option
					  v-for="dict in dict.type.sys_unit"
					  :key="dict.value"
					  :label="dict.label"
					  :value="parseInt(dict.value)"
					></el-option>
				  </el-select>
			</el-col>
       </el-row>
	   
	   <el-row style="margin-bottom: 20px;">
	     	<el-col>
	   		<el-form-item label="产品规格" prop="specification" label-width="50" style="margin: 0px;"/>
	   		<el-input v-model="form.specification" placeholder="请输入规格" style="width: 75%;"/>
	     	</el-col>
	   </el-row>
       
		  
		<el-row style="margin-bottom: 20px;">
		  	<el-col>
				 <el-form-item label="产品材料" prop="material" label-width="50" style="margin: 0px;"/>
				<el-input v-model="form.material" placeholder="请输入材料" style="width: 75%;"/>
		  	</el-col>
		</el-row>
		
       
      <el-row style="margin-bottom: 20px;">
        	<el-col>
				<el-form-item label="产品颜色" prop="color" label-width="50" style="margin: 0px;"/>
				<el-input v-model="form.color" placeholder="请输入颜色" style="width: 75%;"/>
        	</el-col>
      </el-row>
      
       <el-row style="margin-bottom: 20px;">
         	<el-col>
       		  <el-form-item label="产品图片" prop="image" label-width="50" style="margin: 0px;"/>
       		  <image-upload v-model="form.image" style="width: 75%;"/>
         	</el-col>
       </el-row>
       
	   <el-row style="margin-bottom: 20px;">
	     	<el-col>
			<el-form-item label="提交时间" prop="submissionTime" label-width="50" style="margin: 0px;"/>
			  <el-date-picker clearable
				v-model="form.submissionTime"
				type="date"
				value-format="yyyy-MM-dd"
				placeholder="请选择提交时间" style="width: 75%;">
			  </el-date-picker>
			</el-col>
       </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<style>
	::-webkit-scrollbar {
	  width: 10px;
	  height: 15px;
	} 
	::-webkit-scrollbar-thumb{
		border-radius: 3px;
		background-color: #c1c1c1;
	}
</style>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/material/record";

export default {
  name: "Record",
  dicts: ['sys_product_classification', 'sys_unit', 'sys_product_category'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料档案表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productNumber: null,
        productName: null,
        productModel: null,
        productType: null,
        productCategory: null,
        unit: null,
        specification: null,
        material: null,
        color: null,
        image: null,
        submitter: null,
        submissionTime: null,
        updateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物料档案列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productNumber: null,
        productName: null,
        productModel: null,
        productType: null,
        productCategory: null,
        unit: null,
        specification: null,
        material: null,
        color: null,
        image: null,
        submitter: null,
        submissionTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.productNumber)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料档案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const productNumber = row.productNumber || this.ids
      getRecord(productNumber).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料档案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.productNumber != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productNumbers = row.productNumber || this.ids;
      this.$modal.confirm('是否确认删除物料档案编号为"' + productNumbers + '"的数据项？').then(function() {
        return delRecord(productNumbers);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('material/record/export', {
        ...this.queryParams
      }, `record_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
